package com.macro.mall.portal.controller.klxx;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.applet.*;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.MemberClockService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.vo.applet.AppletMemberSignVO;
import com.macro.mall.vo.applet.AppletRankVO;
import com.macro.mall.vo.applet.AppletStatsVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xq
 * @description MemberClockController
 * @date 2024/2/23 13:50
 */
@RestController
@RequestMapping(value = "/member/clock")
public class MemberClockController extends BaseController {

    @Resource
    private MemberClockService memberClockService;

    @Resource
    private UmsMemberService memberService;

    @PostMapping("/signList")
    @ApiOperation(value = "已签到列表")
    public CommonResult<AppletMemberSignVO> signList(@Validated @RequestBody QuerySignListDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        return CommonResult.success(memberClockService.signList(req));

    }

    @PostMapping("/sign")
    @ApiOperation(value = "用户打卡签到")
    public CommonResult<Integer> sign(@Validated @RequestBody AppletSignDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        return memberClockService.sign(req);
    }

    @PostMapping("/apply-holiday")
    @ApiOperation(value = "用户请假")
    public CommonResult<Integer> applyHoliday(@Validated @RequestBody AppletHolidayDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        if (null == req.getTimeInterval()) {
            req.setTimeInterval(60);
        }
        return memberClockService.applyHoliday(req);
    }

    @ApiOperation("会员修改名称")
    @RequestMapping(value = "/updateUserName", method = RequestMethod.PUT)
    public CommonResult updateMemberUserName(@Validated @RequestBody AppletUpdateUserDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        memberService.updateMemberUserName(req);
        UmsMember umsMember = memberService.getById(req.getMemberId());
        return CommonResult.success(umsMember, "修改成功");

    }

    @ApiOperation("会员修改邮箱")
    @RequestMapping(value = "/updateUserEmail", method = RequestMethod.PUT)
    public CommonResult updateUserEmail(@Validated @RequestBody AppletUpdateUserEmailDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        memberService.updateMemberUserEmail(req);
        UmsMember umsMember = memberService.getById(req.getMemberId());
        return CommonResult.success(umsMember, "修改成功");

    }


    @ApiOperation("获取打卡数据")
    @GetMapping(value = "/statsClockData")
    public CommonResult<AppletStatsVO> statsClockData() {
        AppletStatsVO res = memberClockService.statsClockData(this.getCurrentMember().getId());
        return CommonResult.success(res);

    }

    @ApiOperation(value = "获取打卡详情")
    @GetMapping(value = "/clockDetail")
    public CommonResult clockDetail(AppletClockDetailDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        String res = memberClockService.clockDetail(req);
        return CommonResult.success(res);
    }


    /**
     * 获取排序列表
     */
    @ApiOperation("获取打卡排行榜")
    @GetMapping(value = "/rankList")
    public CommonResult<List<AppletRankVO>> rankList(AppletRankDTO req) {
        req.setMemberId(this.getCurrentMember().getId());
        return CommonResult.success(memberClockService.rankList(req));
    }


}

